package com.lagou.edu.service.impl;


import com.lagou.edu.service.EmailService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

/**
 * @Author 梅松
 * @Date 2021/10/17 20:25
 */
@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private MailSender mailSender;
    @Value("${spring.mail.username}")
    private String from;


    /**
     * @方法名: sendMail
     * @参数名：@param subject  邮件主题
     * @参数名：@param content 邮件主题内容
     * @参数名：@param from        发件人Email地址
     * @参数名：@param to         收件人Email地址
     * @描述语: 发送邮件
     */
    @Override
    public Boolean sendSimpleEmail(String email,String checkCode,int expireTime){
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo(email);
        String subject = "注册码";
        String message = "验证码："+ checkCode+",有效期"+expireTime+"秒";
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(message);
        // 发送
        try {
            mailSender.send(simpleMailMessage);
        } catch (MailException e) {
            return false;
        }
        System.out.println("发送完邮件了");
        return true;
    }
}
